package com.leet.code.lcof;

import java.util.HashMap;
import java.util.Map;

public class Code39 {

    public static void main(String[] args) {

        int[] ii = {3, 3, 4};
        int i = new Code39().majorityElement(ii);
        System.out.println(i);
    }

    public int majorityElement(int[] nums) {
        if (nums.length <= 2) {
            return nums[0];
        }
        int l = (nums.length >> 1) + 1;
        Map<Integer, Integer> map = new HashMap<>();
        for (int i = 0; i < nums.length; i++) {
            Integer m = map.get(nums[i]);
            if (m == null) {
                if (i >= l) {
                    continue;
                }
                map.put(nums[i], 1);
            } else {
                int mm = m + 1;
                if (l == mm) {
                    return nums[i];
                }
                map.put(nums[i], mm);

            }
        }
        return nums[nums.length - 1];
    }

}
